package com.lyfam.vaadin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import com.lyfam.vaadin.formfactory.ClientFormFactory;
import com.lyfam.vaadin.formfactory.DealFormFactory;
import com.lyfam.vaadin.formfactory.DealFormFactoryHelper;
import com.lyfam.vaadin.model.Client;
import com.lyfam.vaadin.model.Deal;
import com.lyfam.vaadin.service.DealRegistrationRepository;
import com.vaadin.Application;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.Table.ColumnGenerator;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class MyVaadinApplication extends Application
{
    private Window window;
    private DealRegistrationRepository dao;
    private VerticalLayout layout;
    
    @Override
    public void init()
    {
        window = new Window("Deal Registration");
        setMainWindow(window);
        
        initLayout();
    }
    
    private void initLayout()
    {
        layout = new VerticalLayout();
        window.addComponent(layout);
        
        MenuBar menubar = new MenuBar();
        MenuItem file = menubar.addItem("File", null);
        file.addItem("Register new deal...", newDealCommand);
        file.addSeparator();
        
        file.addItem("Logout", null);
        
        MenuItem view = menubar.addItem("View", null);
        view.addItem("My registered deals...", viewMyDealsCommand);
        
        layout.addComponent(menubar);
    }
    
    private Command newDealCommand = new Command()
    {
        @Override
        public void menuSelected(MenuItem selectedItem)
        {
            final Deal deal = new Deal();
            
            DealFormFactoryHelper formHelper = new DealFormFactoryHelper(deal);
            formHelper.getFormFactory().getprojectName().setNullRepresentation("");
            
            ClientFormFactory clientForm = formHelper.getFormFactory().getclientFormFactory();
            clientForm.getForm().setVisibleItemProperties(new String[] {"name"});
            
            final Form dealForm = formHelper.getFormFactory().getForm();
            Panel panel = formHelper.getFormWizard().getPanel();
            
            final Window subwindow = new Window("Deal Registration");
            subwindow.setWidth("640px");
            subwindow.setHeight("480px");
            subwindow.setPositionX(300);
            subwindow.setPositionY(150);
            
            subwindow.addComponent(panel);
            
            final Button butt = new Button("Save");
            subwindow.addComponent(butt);
            
            final Label lab = new Label();
            subwindow.addComponent(lab);
            
            final DealRegistrationRepository dao = MyVaadinApplication.this.dao;
            butt.addListener(new ClickListener()
            {
                public void buttonClick(ClickEvent event)
                {
                    dealForm.commit();
                    
                    try
                    {
                        dao.saveDeal(deal);
                        
                        lab.setValue(deal.toString());
                        butt.setComponentError(null);
                    }
                    catch (Exception crap)
                    {
                        crap.printStackTrace();
                    }
                }
            });
            
            window.addWindow(subwindow);
        }
    };
    
    private Command viewMyDealsCommand = new Command()
    {
        
        @Override
        public void menuSelected(MenuItem selectedItem)
        {
            final Window subwindow = new Window("My Registered Deals");
            subwindow.setWidth("480px");
            subwindow.setHeight("430px");
            subwindow.setPositionX(100);
            subwindow.setPositionY(50);
            
            final Table dealsTable = new Table("My Deals");
            dealsTable.setSelectable(true);
            dealsTable.setMultiSelect(false);
            dealsTable.setColumnCollapsingAllowed(true);
            dealsTable.addGeneratedColumn("clientName", new ColumnGenerator()
            {
                @Override
                public Component generateCell(Table source, Object itemId, Object columnId)
                {
                    Item item = source.getItem(itemId);
                    Client client = (Client) item.getItemProperty("client").getValue();
                    return new Label(client.getName());
                }
            });
            
            try
            {
                dealsTable.setContainerDataSource(new BeanItemContainer<Deal>(dao.listDeals()));
                dealsTable.setVisibleColumns(new String[]{"dealId", "projectName", "clientName", "product",
                        "subproduct", "relatedProduct"});
                dealsTable.setColumnCollapsed("dealId", true);
            }
            catch (Exception crap)
            {
                crap.printStackTrace();
            }
            
            subwindow.addComponent(dealsTable);
            
            Button refresh = new Button("Refresh");
            refresh.addListener(new ClickListener()
            {
                @Override
                public void buttonClick(ClickEvent event)
                {
                    try
                    {
                        dealsTable.setContainerDataSource(new BeanItemContainer<Deal>(dao.listDeals()));
                        dealsTable.setVisibleColumns(new String[]{"dealId", "projectName", "clientName", "product",
                                "subproduct", "relatedProduct"});
                        dealsTable.setColumnCollapsed("dealId", true);
                    }
                    catch (Exception crap)
                    {
                        crap.printStackTrace();
                    }
                }
            });
            
            subwindow.addComponent(refresh);
            
            window.addWindow(subwindow);
        }
    }; 
    
    public DealRegistrationRepository getDao()
    {
        return dao;
    }

    public void setDao(DealRegistrationRepository dao)
    {
        this.dao = dao;
    }
}
